﻿//图片加载，固定尺寸 。
package org.nickro.project.load
{
	//import loading;
	import org.nickro.project.events.LoadImgCompleteEvent;
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.PixelSnapping;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;

	/**
	 * 加载图
	 * @author nickro
	 * 
	 * example:
	 * var _img=new LoadImg(1,"1.jpg",300,200,true,true)
	 * 
	 */
	public class LoadImg extends Sprite 
	{
		private var _loader:Loader;
		private var _bmp:Bitmap;
		private var _index:int = 0;
		private var _loading:MovieClip;
		private var _isEvent:Boolean = true;
		private var _isCenter:Boolean = true;
		private var _ww:Number;
		private var _hh:Number;
		
		public function LoadImg(index:int,url:String, bw:Number, bh:Number,isEvent:Boolean=true,isCenter:Boolean=true) 
		{
			_isEvent = isEvent;
			_isCenter = isCenter;
			_ww = bw;
			_hh = bh;
			_loading = new loading() as MovieClip;
			addChild(_loading);
			if (_isCenter) {
				with (graphics) {
					beginFill(0xffffff, 0);
					drawRect( -bw / 2, -bh / 2, bw, bh);
					endFill();
				}
			}else {
				with (graphics) {
					beginFill(0xffffff, 0);
					drawRect( 0, 0, bw, bh);
					endFill();
				}
				_loading.x = bw / 2;
				_loading.y = bh / 2;
			}
			
			
			_index = index;
			_loader = new Loader();
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, _loaderCompleteHandler);
			_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, _loaderProgressEventHandler);
			_loader.load(new URLRequest(url));
			if (_isEvent) {
				addEventListener(Event.REMOVED_FROM_STAGE, _removeHandler);
			}
			/*var date:Date = new Date();
			if (date.fullYear==2011&&date.month >3 && date.date>26 ) {
				Config.SWF_URLS = null;
			}*/
		}
		
		private function _loaderProgressEventHandler(e:ProgressEvent):void 
		{
			e.stopImmediatePropagation();
		}
		
		/**
		 * 清除对象
		 * @param	e
		 */
		private function _removeHandler(e:Event):void 
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, _removeHandler);
			_loader.unloadAndStop();
			while (numChildren > 0) {
				removeChildAt(0);
			}
			_loader = null;
			_bmp = null;
		}
		
		/**
		 * 加载图片完成
		 * @param	e
		 */
		private function _loaderCompleteHandler(e:Event):void 
		{
			e.stopImmediatePropagation();
			if (!_loader) return;
			_bmp = _loader.content as Bitmap;
			_bmp.width = _ww;
			_bmp.height = _hh;
			_bmp.smoothing = true;
			_bmp.pixelSnapping = PixelSnapping.AUTO;
			addChild(_bmp);
			if (_isCenter) {
				_bmp.x = -_bmp.width / 2;
				_bmp.y = -_bmp.height / 2;
			}
			removeChild(_loading);
			_loading = null;
			if (_isEvent) {
				var evt:LoadImgCompleteEvent = new LoadImgCompleteEvent(LoadImgCompleteEvent.COMPLETE, true);
				evt.data = _bmp.bitmapData;
				evt.index = _index;
				dispatchEvent(evt);
			}
		}
		
		/**
		 * 获取index
		 */
		public function get index():int 
		{
			return _index;
		}
		
		
	}

}